import { Component } from 'react';
import withRouter from 'umi/withRouter';

import styles from './index.less';
// import Loader from 'components/Loader'
import { connect } from 'dva'

import Error from '../pages/404';

import { pathMatchRegexp, config } from 'utils'
const { openPages } = config;

// @connect(({ loading }) => ({ loading }))
@withRouter
// @connect(({ global }) => ({ global }))
class Layout extends Component {
  componentDidUpdate(prevProps) {
    if (this.props.location !== prevProps.location) {
      window.scrollTo(0, 0);
    }
  }

  // 路由逻辑： 
  // 1. 当前路由 在路由列表中是否存在，存在则显示  {children} ;否则，显示<Error/>；
  // 2. 系统自动生成的路由必须存在，参考：antd-admin项目中.umirc.js中 routes.exclude过滤
  render() {
    const { props } = this

    const { global, location, dispatch, children } = props

    const routeList = localStorage.getItem('routeList') ? JSON.parse(localStorage.getItem('routeList')) : []

    // Find a route that matches the pathname.
    const currentRoute = routeList.find(
      _ => _.path && pathMatchRegexp(_.path, location.pathname)
    )

    // Query whether you have permission to enter this page
    const hasPermission = currentRoute || openPages.indexOf(location.pathname) >= 0 ? true : false

    return (
      <div className={styles.normal}>
        {/* <Loader fullScreen spinning={loading.effects['global/query']} /> */}
        {hasPermission ? children : <Error />}
      </div>
    );
  }
}

export default withRouter(Layout);
